# 由一堆组件构成一个完整的实体，组件本身独立，但又不能自己运行，必须跟宿主组合在一起运行

class Dog:  # 定义一个狗类
    role = 'dog'  # 狗的角色属性都是狗

    def __init__(self, name, breed, attack_val):
        self.name = name
        self.breed = breed  # 品种
        self.attack_val = attack_val  # 攻击值
        self.life_val = 100

    def bite(self, person_obj):  # 实体
        person_obj.life_val -= self.attack_val
        print('狗[%s]咬了人[%s]一口，人掉血[%s]，还有血量[%s]...' % (
            self.name, person_obj.name, self.attack_val, person_obj.life_val))


class Person:
    def __init__(self, name, sex):
        self.name = name
        self.sex = sex  # 品种
        # self.attack_val = attack_val  # 攻击值
        self.life_val = 100
        self.weapon = Weapon()  # 直接实例化

    '''
    def beat(self, dog_obj):  # 实体
        dog_obj['life_val'] -= self.attack_val
        print('人[%s]打了狗[%s]一下，狗掉血[%s]，还有血量[%s]...' % (
            self.name, dog_obj['name'], self.attack_val, dog_obj['life_val']))
    '''


class Weapon:

    def dog_stick(self, obj):
        '''打狗棒'''
        self.name = '打狗棒'
        self.attack_val = 40
        obj.life_val -= self.attack_val
        self.print_log(obj)

    def knife(self, obj):
        '''屠龙刀'''
        self.name = '屠龙刀'
        self.attack_val = 80
        obj.life_val -= self.attack_val
        self.print_log(obj)

    def gun(self, obj):
        '''AK47'''
        self.name = 'AK47'
        self.attack_val = 100
        obj.life_val -= self.attack_val
        self.print_log(obj)

    def print_log(self, obj):
        print('[%s]被[%s]打了一下，掉血[%s]，还有血量[%s]...' % (
            obj.name, self.name, self.attack_val, obj.life_val))


p1 = Person('Alex', 'M')
d1 = Dog('mjj', '京巴', 30)
d1.bite(p1)
# p1.weapon.dog_stick(d1)
# p1.weapon.knife(d1)
p1.weapon.gun(d1)
